package com.huixuebao.api.util.pay;

import com.huixuebao.api.dto.PayAmountComplexResult;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
@Slf4j
public class PayUtil {

    public static PayAmountComplexResult calcPayAmount(BigDecimal coursePrice, BigDecimal settlePrice) {
        PayAmountComplexResult payAmountComplexResult = new PayAmountComplexResult();
        BigDecimal percentage = BigDecimal.ZERO;
        coursePrice = coursePrice.add(coursePrice.multiply(new BigDecimal("0.2"))).setScale(2, BigDecimal.ROUND_HALF_UP);
        log.info("课程价格"+ coursePrice);
        if (settlePrice.compareTo(new BigDecimal("5000")) >= 0) {
            percentage = new BigDecimal("0.5");
        } else if (settlePrice.compareTo(new BigDecimal("5000")) < 0 && settlePrice.compareTo(new BigDecimal("2000")) >= 0) {
            percentage = new BigDecimal("0.7");
        }

        payAmountComplexResult.setCourseAmount(coursePrice);
        payAmountComplexResult.setSettleAmount(settlePrice);
        payAmountComplexResult.setPercentage(percentage);

        return payAmountComplexResult;
    }

    public static boolean validatePayAmount(BigDecimal coursePrice, BigDecimal settlePrice, BigDecimal payAmount) {
        PayAmountComplexResult payAmountComplexResult = calcPayAmount(coursePrice, settlePrice);
        BigDecimal localPayAmount = payAmountComplexResult.getCourseAmount().subtract(payAmountComplexResult.getSettleAmount().multiply(payAmountComplexResult.getPercentage())).setScale(2, BigDecimal.ROUND_HALF_UP);
        localPayAmount = localPayAmount.multiply(new BigDecimal(100));
        log.info("hha"+ localPayAmount.compareTo(payAmount));
        if (localPayAmount.compareTo(BigDecimal.ZERO) > 0 && localPayAmount.compareTo(payAmount) == 0) {
            return true;
        }
        return false;
    }
}
